<?php
	require_once(CUTEENGINE_BASE_COMPONENTS_PATH."base_component_class.php");
    //
    // t_rss_generator
    //
class t_rss_generator_channel
{
    var $atomLinkHref = '';
    var $title = '';
    var $link = '';
    var $description = '';
    var $language = '';
    var $copyright = '';
    var $managingEditor = '';
    var $webMaster = '';
    var $pubDate = '';
    var $lastBuildDate = '';
    var $categories = array();
    var $generator = '';
    var $docs = '';
    var $ttl = '';
    var $image = '';
    var $textInput = '';
    var $skipHours = array();
    var $skipDays = array();
    var $cloud_domain = '';
    var $cloud_port = '80';
    var $cloud_path = '';
    var $cloud_registerProcedure = '';
    var $cloud_protocol = '';
    var $items = array();
    var $extraXML = '';

}

class t_rss_generator_image
{
    var $url = '';
    var $title = '';
    var $link = '';
    var $width = '88';
    var $height = '31';
    var $description = '';

}

class t_rss_generator_text_input
{
    var $title = '';
    var $description = '';
    var $name = '';
    var $link = '';

}

class t_rss_generator_item
{
    var $title = '';
    var $description = '';
    var $link = '';
    var $author = '';
    var $pubDate = '';
    var $comments = '';
    var $guid = '';
    var $guid_isPermaLink = false;
    var $source = '';
    var $source_url = '';
    var $enclosure_url = '';
    var $enclosure_length = '0';
    var $enclosure_type = '';
    var $categories = array();

}

class t_rss_generator extends t_base_component
{
        // property declaration
        // public
		// E
		var $encoding = '';
        // M
        // argument field name
		const main_url_argument_instance_no = "instance_no";
        // argument method name
		const main_url_method_create_rss = "create_rss";
        const main_url_method_create_rss_label = "create_rss";
		// db component parameter name
		const main_query_component_source_data_query = "source_data_query";
		const main_query_component_title = "rss_title";
		const main_query_component_link = "rss_link";
		const main_query_component_description = "rss_description";
		const main_query_component_language = 'rss_language';
        const main_query_component_managingEditor = 'rss_managingEditor';
        const main_query_component_webMaster = 'rss_webMaster'; 
		
		// R
        var $rss_channel = false; //t_rss_generator_channel
		const rss_query_parameter_query_repository_name = "rss_query_query";
		public $rss_query_query = false; // t_query_parameter
        var $rss_version = '2.0';
		// S
		const specific_parameters_position_source_data_query = 1;
		const specific_parameters_position_title = 2;
		const specific_parameters_position_link = 3;
		const specific_parameters_position_description = 4;
		const specific_parameters_position_language = 5;
        const specific_parameters_position_managingEditor = 6;
        const specific_parameters_position_webMaster = 7; 		
        const specific_parameters_position_last = 8;
		
		var $stylesheet = '';
		// protected method
		// G
        static function get_help_specific_parameter($a_index)
		{
			$local_return = 'NO HELP AVAILABLE';
			/*switch ($a_index) 
            {
                case self::specific_parameters_position_:
					$local_return = '';
				break;                
			}*/
			return $local_return;
		}
        static function get_default_specific_parameter($a_index)
		{
			$local_return = '';
			//echo "a_index: $a_index<br>";
			switch ($a_index) 
            {
                /*case self::specific_parameters_position_:
					$local_return = 'TRUE';
				break;                */
			}
			return $local_return;
		}	
        protected function get_specific_parameters_position_last()
        {
            return self::specific_parameters_position_last;
        }   
		// I
		protected function initialize_query_parameters()
		{
		    // db array parameter									
			$this->new_query_parameter(
				t_cuteengine_component::main_query_parameter_name,
				self::main_query_component_source_data_query,
				t_cuteengine_component::main_query_field_name_specific_parameters,"1");
			$this->new_query_parameter(
				t_cuteengine_component::main_query_parameter_name,
				self::main_query_component_title,
				t_cuteengine_component::main_query_field_name_specific_parameters,"2");
			$this->new_query_parameter(
				t_cuteengine_component::main_query_parameter_name,
				self::main_query_component_link,
				t_cuteengine_component::main_query_field_name_specific_parameters,"3");				
			$this->new_query_parameter(
				t_cuteengine_component::main_query_parameter_name,
				self::main_query_component_description,
				t_cuteengine_component::main_query_field_name_specific_parameters,"4");	
			$this->new_query_parameter(
				t_cuteengine_component::main_query_parameter_name,
				self::main_query_component_language,
				t_cuteengine_component::main_query_field_name_specific_parameters,"5");
			$this->new_query_parameter(
				t_cuteengine_component::main_query_parameter_name,
				self::main_query_component_managingEditor,
				t_cuteengine_component::main_query_field_name_specific_parameters,"6");
			$this->new_query_parameter(
				t_cuteengine_component::main_query_parameter_name,
				self::main_query_component_webMaster,
				t_cuteengine_component::main_query_field_name_specific_parameters,"7");	
		}
		// public
		public function __construct($a_cuteengine_component) 
		{
            parent::__construct($a_cuteengine_component);
           // echo "CREATA classe ".get_class($this);
            
			$this->default_method = 'create_rss';
            // url argouments
			$this->new_url_argument(self::main_url_argument_instance_no,
				0);
            // url Methods
			$this->new_url_method(self::main_url_method_create_rss,
                self::main_url_method_create_rss_label);
			
						

		}
		// C
        public function create_rss_channel()
        {
            $this->rss_channel = new t_rss_generator_channel;
            $this->rss_channel->atomLinkHref = '';
			$this->rss_channel->title = $this->get_main_query_parameter_value(self::main_query_component_title);//'Annunci cds';
            $this->rss_channel->link = $this->get_main_query_parameter_value(self::main_query_component_link);//'http://www.associazioneilraggio.it';
            $this->rss_channel->description = $this->get_main_query_parameter_value(self::main_query_component_description);//'Domande di lavoro';
            $this->rss_channel->language = $this->get_main_query_parameter_value(self::main_query_component_language);//'it-IT';
            $this->rss_channel->generator = 'ciao paolo';//'CuteEngine RSS Feed Generator';
            $this->rss_channel->managingEditor = $this->get_main_query_parameter_value(self::main_query_component_managingEditor);//'';
            $this->rss_channel->webMaster = $this->get_main_query_parameter_value(self::main_query_component_webMaster);//;
			$this->rss_channel->ttl = '1';
			$this->rss_channel->lastBuildDate = gmdate(DATE_RFC822, time());
			

        }    
        
		// E
		public function execute_component_action()
		{
			$local_function = parent::execute_component_action();
			//echo "this->url_method(): ".$this->url_method()." - local_function: $local_function<br>";
            if (trim($local_function) != '')
			{
				$this->$local_function();
            }
			else
			{
			}
				
			//echo "argomento: ".$this->url_argument(self::main_url_argument_import_file_name);
			//$this->parse_source();
		}	
		
		// C
		function c_data($str)
		{
			return '<![CDATA[ ' . $str . ' ]]>';
		}
		
		function create_rss()
		{
		    Global $g_ambient_namespace;
			$local_sql = $this->get_main_query_parameter_value(self::main_query_component_source_data_query);
           // echo $local_sql;
			$this->new_query(
				self::rss_query_parameter_query_repository_name,
				$local_sql,$g_ambient_namespace->connection);
			//echo "1\n";
            $rss_query_query = $this->get_query(
				self::rss_query_parameter_query_repository_name);
           // echo "2\n";
            $local_function = "new_rss_item";    
            //echo "3\n";
            $this->create_rss_channel();
			$rss_query_query->execute_function_from_query_result($this,$local_function);
            //echo "4\n";
            $this->encoding = 'UTF-8';
            $this->version = '2.0';
			ob_end_clean();
			ob_start('ob_gzhandler',1) ;
            header('Content-Type: text/xml');
            echo $this->createFeed($this->rss_channel); 
		}

    function createFeed($channel)
    {
		$selfUrl = (!array_key_exists('HTTPS',$_SERVER) || $_SERVER['HTTPS'] != 'on' ? 'http://' : 'https://');
        $selfUrl .= $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']/*."?".$_SERVER['QUERY_STRING']*/;
        $rss = '<?xml version="1.0"';
        if (!empty($this->encoding)) {
            $rss .= ' encoding="' . $this->encoding . '"';
        }
        $rss .= '?>' . "\n";
        if (!empty($this->stylesheet)) {
            $rss .= $this->stylesheet . "\n";
        }
        $rss .= '<!-- Generated on ' . date('r') . ' -->' . "\n";
        $rss .= '<rss version="' . $this->rss_version . '" xmlns:atom="http://www.w3.org/2005/Atom">' . "\n";
        $rss .= '  <channel>' . "\n";
        $rss .= '    <atom:link href="' . ($channel->atomLinkHref ? $channel->atomLinkHref : $selfUrl) . '" rel="self" type="application/rss+xml" />' . "\n";
        $rss .= '    <title>' . $channel->title . '</title>' . "\n";
        $rss .= '    <link>' . $channel->link . '</link>' . "\n";
        $rss .= '    <description>' . $channel->description . '</description>' . "\n";
        if (!empty($channel->language)) {
            $rss .= '    <language>' . $channel->language . '</language>' . "\n";
        }
        if (!empty($channel->copyright)) {
            $rss .= '    <copyright>' . $channel->copyright . '</copyright>' . "\n";
        }
        if (!empty($channel->managingEditor)) {
            $rss .= '    <managingEditor>' . $channel->managingEditor . '</managingEditor>' . "\n";
        }
        if (!empty($channel->webMaster)) {
            $rss .= '    <webMaster>' . $channel->webMaster . '</webMaster>' . "\n";
        }
        if (!empty($channel->pubDate)) {
            $rss .= '    <pubDate>' . $channel->pubDate . '</pubDate>' . "\n";
        }
        if (!empty($channel->lastBuildDate)) {
            $rss .= '    <lastBuildDate>' . $channel->lastBuildDate . '</lastBuildDate>' . "\n";
        }
        foreach ($channel->categories as $category) {
            $rss .= '    <category';
            if (!empty($category['domain'])) {
                $rss .= ' domain="' . $category['domain'] . '"';
            }
            $rss .= '>' . $category['name'] . '</category>' . "\n";
        }
        if (!empty($channel->generator)) {
            $rss .= '    <generator>' . $channel->generator . '</generator>' . "\n";
        }
        if (!empty($channel->docs)) {
            $rss .= '    <docs>' . $channel->docs . '</docs>' . "\n";
        }
        //if (!empty($channel->ttl)) {
            $rss .= '    <ttl>' . $channel->ttl . '</ttl>' . "\n";
        //}
        if (sizeof($channel->skipHours)) {
            $rss .= '    <skipHours>' . "\n";
            foreach ($channel->skipHours as $hour) {
                $rss .= '      <hour>' . $hour . '</hour>' . "\n";
            }
            $rss .= '    </skipHours>' . "\n";
        }
        if (sizeof($channel->skipDays)) {
            $rss .= '    <skipDays>' . "\n";
            foreach ($channel->skipDays as $day) {
                $rss .= '      <day>' . $day . '</day>' . "\n";
            }
            $rss .= '    </skipDays>' . "\n";
        }
        if (!empty($channel->image)) {
            $image = $channel->image;
            $rss .= '    <image>' . "\n";
            $rss .= '      <url>' . $image->url . '</url>' . "\n";
            $rss .= '      <title>' . $image->title . '</title>' . "\n";
            $rss .= '      <link>' . $image->link . '</link>' . "\n";
            if ($image->width) {
                $rss .= '      <width>' . $image->width . '</width>' . "\n";
            }
            if ($image->height) {
                $rss .= '      <height>' . $image->height . '</height>' . "\n";
            }
            if (!empty($image->description)) {
                $rss .= '      <description>' . $image->description . '</description>' . "\n";
            }
            $rss .= '    </image>' . "\n";
        }
        if (!empty($channel->textInput)) {
            $textInput = $channel->textInput;
            $rss .= '    <textInput>' . "\n";
            $rss .= '      <title>' . $textInput->title . '</title>' . "\n";
            $rss .= '      <description>' . $textInput->description . '</description>' . "\n";
            $rss .= '      <name>' . $textInput->name . '</name>' . "\n";
            $rss .= '      <link>' . $textInput->link . '</link>' . "\n";
            $rss .= '    </textInput>' . "\n";
        }
        if (!empty($channel->cloud_domain) || !empty($channel->cloud_path) || !empty($channel->cloud_registerProcedure) || !empty($channel->cloud_protocol)) {
            $rss .= '    <cloud domain="' . $channel->cloud_domain . '" ';
            $rss .= 'port="' . $channel->cloud_port . '" path="' . $channel->cloud_path . '" ';
            $rss .= 'registerProcedure="' . $channel->cloud_registerProcedure . '" ';
            $rss .= 'protocol="' . $channel->cloud_protocol . '" />' . "\n";
        }
        if (!empty($channel->extraXML)) {
            $rss .= $channel->extraXML . "\n";
        }
        foreach ($channel->items as $item) {
            $rss .= '    <item>' . "\n";
            if (!empty($item->title)) {
                $rss .= '      <title>' . $item->title . '</title>' . "\n";
            }
            if (!empty($item->description)) {
                $rss .= '      <description>' . $item->description . '</description>' . "\n";
            }
            if (!empty($item->link)) {
                $rss .= '      <link>' . $item->link . '</link>' . "\n";
            }
            if (!empty($item->pubDate)) {
                $rss .= '      <pubDate>' . $item->pubDate . '</pubDate>' . "\n";
            }
            if (!empty($item->author)) {
                $rss .= '      <author>' . $item->author . '</author>' . "\n";
            }
            if (!empty($item->comments)) {
                $rss .= '      <comments>' . $item->comments . '</comments>' . "\n";
            }
            if (!empty($item->guid)) {
                $rss .= '      <guid isPermaLink="';
                $rss .= ($item->guid_isPermaLink ? 'true' : 'false') . '">';
                $rss .= $item->guid . '</guid>' . "\n";
            }
            if (!empty($item->source)) {
                $rss .= '      <source url="' . $item->source_url . '">';
                $rss .= $item->source . '</source>' . "\n";
            }
            if (!empty($item->enclosure_url) || !empty($item->enclosure_type)) {
                $rss .= '      <enclosure url="' . $item->enclosure_url . '" ';
                $rss .= 'length="' . $item->enclosure_length . '" ';
                $rss .= 'type="' . $item->enclosure_type . '" />' . "\n";
            }
            foreach ($item->categories as $category) {
                $rss .= '      <category';
                if (!empty($category['domain'])) {
                    $rss .= ' domain="' . $category['domain'] . '"';
                }
                $rss .= '>' . $category['name'] . '</category>' . "\n";
            }
            $rss .= '    </item>' . "\n";
        }
        $rss .= '  </channel>' . "\r";
        return $rss .= '</rss>';
    }
    
        // N
        public function new_rss_item($a_query_parameter)
        {
            //echo "entrato new_rss_item";
			$this->log->add_log_on_page("entrato new_rss_item",true);
           // echo $a_query_parameter->sql_values["description"]."\n";
           // echo $a_query_parameter->sql_values["pub_date"]."\n";
            $item = new t_rss_generator_item();
            $item->title = $this->c_data($a_query_parameter->sql_values[0]);
            $item->description = $this->c_data($a_query_parameter->sql_values[1]);
            $item->link = $a_query_parameter->sql_values[2];
            $item->guid = $a_query_parameter->sql_values[3];
            $item->pubDate = gmdate(DATE_RFC822, strtotime($a_query_parameter->sql_values[4]));
            $this->rss_channel->items[] = $item;
        }    

}



?> 